package com.hss01248.dialog.ios;

import ohos.agp.components.*;
import ohos.agp.components.Text.TextSizeType;

import com.hss01248.dialog.ResourceTable;
import com.hss01248.dialog.Tool;
import com.hss01248.dialog.adapter.SuperLvHolder;
import com.hss01248.dialog.config.ConfigBean;
import com.hss01248.dialog.config.DefaultConfig;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.app.Context;

/**
 * Created by Administrator on 2016/10/9 0009.
 */
public class IosAlertDialogHolder extends SuperLvHolder<ConfigBean> {
    protected Text tvTitle;
    public Text tvMsg;
    public TextField et1;
    public TextField et2;
    protected Component line;
    protected Button btn1;
    protected Component lineBtn2;
    protected Button btn2;
    protected Component lineBtn3;
    protected Button btn3;
    protected DirectionalLayout llContainerHorizontal;
    protected Button btn1Vertical;
    protected Component lineBtn2Vertical;
    protected Button btn2Vertical;
    protected Component lineBtn3Vertical;
    protected Button btn3Vertical;
    protected DirectionalLayout llContainerVertical;
    protected ScrollView sv;
    protected DirectionalLayout llContainerContent;
    ConfigBean bean;



    public IosAlertDialogHolder(Context context) {
        super(context);
    }

    @Override
    protected void findViews() {
        tvTitle = (Text) rootView.findComponentById(ResourceTable.Id_tv_title);
        tvMsg = (Text) rootView.findComponentById(ResourceTable.Id_tv_msg);
        tvMsg.setMultipleLine(true);
        et1 = (TextField) rootView.findComponentById(ResourceTable.Id_iso_et_1);
        et2 = (TextField) rootView.findComponentById(ResourceTable.Id_iso_et_2);
        line = (Component) rootView.findComponentById(ResourceTable.Id_line);
        btn1 = (Button) rootView.findComponentById(ResourceTable.Id_btn_1);
        lineBtn2 = (Component) rootView.findComponentById(ResourceTable.Id_line_btn2);
        btn2 = (Button) rootView.findComponentById(ResourceTable.Id_btn_2);
        lineBtn3 = (Component) rootView.findComponentById(ResourceTable.Id_line_btn3);
        btn3 = (Button) rootView.findComponentById(ResourceTable.Id_btn_3);
        llContainerHorizontal = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_ll_container_horizontal);
        btn1Vertical = (Button) rootView.findComponentById(ResourceTable.Id_btn_1_vertical);
        lineBtn2Vertical = (Component) rootView.findComponentById(ResourceTable.Id_line_btn2_vertical);
        btn2Vertical = (Button) rootView.findComponentById(ResourceTable.Id_btn_2_vertical);
        lineBtn3Vertical = (Component) rootView.findComponentById(ResourceTable.Id_line_btn3_vertical);
        btn3Vertical = (Button) rootView.findComponentById(ResourceTable.Id_btn_3_vertical);
        llContainerVertical = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_ll_container_vertical);
        sv = (ScrollView) rootView.findComponentById(ResourceTable.Id_sv);
        llContainerContent = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_ll_container);
    }

    public void showOnlyTitleAndMsg(String title, String msg){
        et2.setVisibility(Component.HIDE);
        et1.setVisibility(Component.HIDE);
        llContainerHorizontal.setVisibility(Component.HIDE);
        llContainerVertical.setVisibility(Component.HIDE);
        line.setVisibility(Component.HIDE);
        tvTitle.setText(title);
        tvMsg.setText(msg);
    }


    @Override
    protected int setLayoutRes() {
        return ResourceTable.Layout_dialog_ios_alert;
    }

    @Override
    public void assingDatasAndEvents(Context context, final ConfigBean bean) {
        this.bean = bean;
        bean.viewHolder = this;
        //btn style -size,color,oritation
        setBtnsSytle(context,bean);
        //set title style
        setTitleStyle(context,bean);
        //自定义content
        if(bean.customContentHolder ==null){
            //msg
            setMsgStyleAndTxt(bean);
            //input
            setInputStyle(context,bean);
        }else {
            addCustomView(bean,context);
        }
        //按钮数量
        setBtnTxt(context,bean);
        //事件
        if (bean.isVertical) {
            setBtnEventVertical(bean,context);
        } else {
            setBtnEventH(context,bean);
        }
    }

    private void addCustomView(ConfigBean bean, Context context) {
        //设置了content holders时,中央采用自定义view
        tvMsg.setVisibility(Component.HIDE);
        if (et1 != null) et1.setVisibility(Component.HIDE);
        if (et2 != null) et2.setVisibility(Component.HIDE);

        DirectionalLayout.LayoutConfig params = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT);
        SuperLvHolder holder = bean.customContentHolder;
        holder.rootView.setLayoutConfig(params);
        if(holder.rootView.getComponentParent() ==null){
            llContainerContent.addComponent(holder.rootView);
        }else {
            ((ComponentContainer)(holder.rootView.getComponentParent())).removeComponent(holder.rootView);
            llContainerContent.addComponent(holder.rootView);
        }

    }

    private void setBtnEventH(Context context, final ConfigBean bean) {
        btn1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {

                if(bean.type == DefaultConfig.TYPE_IOS_INPUT){
                  boolean  needDismiss =  bean.listener.onInputValid(et1.getText().toString().trim(),et2.getText().toString().trim(),et1,et2);
                  if(!needDismiss){
                      return;
                  }
                  bean.listener.onGetInput(et1.getText().toString().trim(),et2.getText().toString().trim());
                }
                Tool.dismiss(bean,true);
                bean.listener.onFirst();

            }
        });

        btn2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {

                Tool.dismiss(bean);
                bean.listener.onSecond();
            }
        });

        btn3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                Tool.dismiss(bean);
                bean.listener.onThird();
            }
        });
    }

    private void setBtnEventVertical(final ConfigBean bean, Context context) {
        btn1Vertical.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {

                if(bean.type == DefaultConfig.TYPE_IOS_INPUT){
                    boolean  needDismiss =  bean.listener.onInputValid(et1.getText().toString().trim(),et2.getText().toString().trim(),et1,et2);
                    if(!needDismiss){
                        return;
                    }
                    bean.listener.onGetInput(et1.getText().toString().trim(),et2.getText().toString().trim());
                }
                Tool.dismiss(bean,true);
                bean.listener.onFirst();
            }
        });

        btn2Vertical.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                Tool.dismiss(bean);
                bean.listener.onSecond();
            }
        });

        btn3Vertical.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                Tool.dismiss(bean);
                bean.listener.onThird();
            }
        });
    }


    private void setMsgStyleAndTxt(ConfigBean bean) {
        if (Tool.isEmpty(bean.msg )) {
            tvMsg.setVisibility(Component.HIDE);
        } else {
            tvMsg.setVisibility(Component.VISIBLE);
            tvMsg.setText(bean.msg.toString());
            Tool.setTextColorById(tvMsg, bean.msgTxtColor);
            tvMsg.setTextSize(bean.msgTxtSize, Text.TextSizeType.FP);
        }
    }

    private void setBtnTxt(Context context, ConfigBean bean) {
        if (Tool.isEmpty(bean.text3 )) {
            if (bean.isVertical) {
                btn3Vertical.setVisibility(Component.HIDE);
                lineBtn3Vertical.setVisibility(Component.HIDE);
                StateElement se = new StateElement();
                se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, new ShapeElement(context, ResourceTable.Graphic_bg_ios_roundcorner_all_bottom_gray));
                se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new ShapeElement(context, ResourceTable.Graphic_bg_ios_roundcorner_all_bottom_white));
                btn2Vertical.setBackground(se);
            } else {
                btn3.setVisibility(Component.HIDE);
                lineBtn3.setVisibility(Component.HIDE);
                StateElement se = new StateElement();
                se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, new ShapeElement(context, ResourceTable.Graphic_bg_ios_roundcorner_right_bottom_gray));
                se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new ShapeElement(context, ResourceTable.Graphic_bg_ios_roundcorner_right_bottom_white));
                btn2.setBackground(se);
            }

        } else {
            if (bean.isVertical) {
                btn3Vertical.setVisibility(Component.VISIBLE);
                lineBtn3Vertical.setVisibility(Component.VISIBLE);
                btn3Vertical.setText(bean.text3.toString());
            } else {
                btn3.setVisibility(Component.VISIBLE);
                lineBtn3.setVisibility(Component.VISIBLE);
                btn3.setText(bean.text3.toString());
            }
        }

        if (Tool.isEmpty(bean.text2 )) {
            if (bean.isVertical) {
                btn2Vertical.setVisibility(Component.HIDE);
                lineBtn2Vertical.setVisibility(Component.HIDE);
                StateElement se = new StateElement();
                se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, new ShapeElement(context, ResourceTable.Graphic_bg_ios_roundcorner_all_bottom_gray));
                se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new ShapeElement(context, ResourceTable.Graphic_bg_ios_roundcorner_all_bottom_white));
                btn1Vertical.setBackground(se);
            } else {
                btn2.setVisibility(Component.HIDE);
                lineBtn2.setVisibility(Component.HIDE);
                StateElement se = new StateElement();
                se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, new ShapeElement(context, ResourceTable.Graphic_bg_ios_roundcorner_all_bottom_gray));
                se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new ShapeElement(context, ResourceTable.Graphic_bg_ios_roundcorner_all_bottom_white));
                btn1.setBackground(se);
            }
        } else {
            if (bean.isVertical) {
                btn2Vertical.setVisibility(Component.VISIBLE);
                lineBtn2Vertical.setVisibility(Component.VISIBLE);
                btn2Vertical.setText(bean.text2.toString());
            } else {
                btn2.setVisibility(Component.VISIBLE);
                lineBtn2.setVisibility(Component.VISIBLE);
                btn2.setText(bean.text2.toString());
            }
        }

        if (bean.isVertical) {
            btn1Vertical.setText(bean.text1.toString());
        } else {

            btn1.setText(bean.text1.toString());
        }
    }

    private void setTitleStyle(Context context, ConfigBean bean) {
        if (Tool.isEmpty(bean.title )) {
            tvTitle.setVisibility(Component.HIDE);
        } else {
            tvTitle.setVisibility(Component.VISIBLE);
            tvTitle.setText(bean.title.toString());
            Tool.setTextColorById(tvTitle, bean.titleTxtColor);
            tvTitle.setTextSize(bean.titleTxtSize, TextSizeType.FP);
        }
    }

    private void setBtnsSytle(Context context, ConfigBean bean) {
        btn3Vertical.setTextSize(bean.btnTxtSize, TextSizeType.FP);
        btn2Vertical.setTextSize(bean.btnTxtSize, TextSizeType.FP);
        btn1Vertical.setTextSize(bean.btnTxtSize, TextSizeType.FP);
        btn3.setTextSize(bean.btnTxtSize, TextSizeType.FP);
        btn2.setTextSize(bean.btnTxtSize, TextSizeType.FP);
        btn1.setTextSize(bean.btnTxtSize, TextSizeType.FP);

        Tool.setTextColorById(btn1, bean.btn1Color);
        Tool.setTextColorById(btn2, bean.btn2Color);
        Tool.setTextColorById(btn3, bean.btn3Color);

        Tool.setTextColorById(btn1Vertical, bean.btn1Color);
        Tool.setTextColorById(btn2Vertical, bean.btn2Color);
        Tool.setTextColorById(btn3Vertical, bean.btn3Color);

        //隐藏view
        if (bean.isVertical) {
            llContainerVertical.setVisibility(Component.VISIBLE);
            llContainerHorizontal.setVisibility(Component.HIDE);
        } else {
            llContainerVertical.setVisibility(Component.HIDE);
            llContainerHorizontal.setVisibility(Component.VISIBLE);
        }
    }

    private void setInputStyle(Context context, ConfigBean bean) {
        if (Tool.isEmpty(bean.hint1 )) {
            if(et1 != null) et1.setVisibility(Component.HIDE);
        } else {
            bean.setNeedSoftKeyboard(true);
            DirectionalLayout.LayoutConfig params = (DirectionalLayout.LayoutConfig)sv.getLayoutConfig();
            if (params == null) {
                params = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
                        ComponentContainer.LayoutConfig.MATCH_CONTENT);
            }
            params.weight = 0;
            params.height = ComponentContainer.LayoutConfig.MATCH_CONTENT;
            sv.setLayoutConfig(params);
            et1.setVisibility(Component.VISIBLE);
            et1.setHint(bean.hint1.toString());

            Tool.setTextColorById(et1, bean.inputTxtColor);
            et1.setTextSize(bean.inputTxtSize, TextSizeType.FP);
            if (Tool.isEmpty(bean.inputText1 )) {
                et1.setText(bean.inputText1.toString());
                et1.setSelected(true);
            }


        }

        if (Tool.isEmpty(bean.hint2 )) {
            if(et2 != null)  et2.setVisibility(Component.HIDE);
        } else {
            bean.setNeedSoftKeyboard(true);
            et2.setVisibility(Component.VISIBLE);
            et2.setHint(bean.hint2.toString());
            Tool.setTextColorById(et2, bean.inputTxtColor);
            et2.setTextSize(bean.inputTxtSize, TextSizeType.FP);
            if (bean.isInput2HideAsPassword) {
                //设置EditText文本为可见的
                et2.setTextInputType(InputAttribute.PATTERN_PASSWORD);
            } else {
                //设置EditText文本为隐藏的
                et2.setTextInputType(InputAttribute.PATTERN_NULL);
            }
            if (Tool.isEmpty(bean.inputText2 )) {
                et2.setText(bean.inputText2.toString());
                et2.setSelected(true);
            }
        }
    }


    @Override
    public void showKeyBoard(){
//弹出软键盘
        if(Tool.isEmpty(bean.hint2) && !Tool.isEmpty(bean.hint1)){
            Tool.showKeyBoard(et1);
        }else if(Tool.isEmpty(bean.hint1) && !Tool.isEmpty(bean.hint2)){
            Tool.showKeyBoard(et2);
        }else if(!Tool.isEmpty(bean.hint2) && !Tool.isEmpty(bean.hint1)){
            Tool.showKeyBoard(et1);
        }

    }

    @Override
    public void hideKeyBoard(){
        if(Tool.isEmpty(bean.hint2) && !Tool.isEmpty(bean.hint1)){
            Tool.hideKeyBoard(et1);
        }else if(Tool.isEmpty(bean.hint1) && !Tool.isEmpty(bean.hint2)){
            Tool.hideKeyBoard(et2);
        }else if(!Tool.isEmpty(bean.hint2) && !Tool.isEmpty(bean.hint1)){
            Tool.hideKeyBoard(et1);
        }
    }




}
